package org.zdlh.btn;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.tool.btn.MoBanBtn;
import com.tool.btn.RefineOperBtn;
import com.tool.tcpimg.UIUtils;
import com.tool.time.Limit;
import com.tool.time.TimeLimit;
import com.tool.time.TimerUtil;
import org.come.Frame.NewRefiningJframe;
import org.come.Frame.WorkshopRefiningJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.NewRefiningJpanel;
import org.come.Jpanel.RefinersJpanel;
import org.come.Jpanel.RefiningEquiJpanel;
import org.come.entity.RefiningValue;
import org.come.model.InternalForm;
import org.come.until.FormsManagement;
import org.come.until.RefiningUtil;
import org.zdlh.Jpanel.ZdlhJpanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZdlhBtn extends MoBanBtn {

    private String FlagMes;

    private ZdlhJpanel zdlhJpanel;

    private RefineOperBtn refineOperBtn;

    public ZdlhBtn(String iconpath, int type, String FlagMes) {
        super(iconpath, type);
        this.FlagMes = FlagMes;
    }

    public ZdlhBtn(final int type, final String FlagMes) {
        super("", type);
        this.FlagMes = FlagMes;
    }

    public ZdlhBtn(final String iconpath, final int type, final String FlagMes, final String text) {
        super(iconpath, type, UIUtils.COLOR_BTNPUTONG2);
        this.FlagMes = FlagMes;
        this.setText(text);
        this.setVerticalTextPosition(SwingConstants.CENTER);
        this.setHorizontalTextPosition(SwingConstants.CENTER);
        this.setFont(UIUtils.TEXT_HY88);
    }

    public ZdlhBtn(final String iconpath, final int type, final String FlagMes, final String text, Font font) {
        super(iconpath, type, UIUtils.COLOR_BTNPUTONG2);
        this.FlagMes = FlagMes;
        this.setText(text);
        this.setVerticalTextPosition(SwingConstants.CENTER);
        this.setHorizontalTextPosition(SwingConstants.CENTER);
        this.setFont(font);
    }

    public ZdlhBtn(String iconpath,  final int type, final String FlagMes, final String text, ZdlhJpanel zdlhJpanel) {
        super(iconpath, type, UIUtils.COLOR_WHITE2);
        this.FlagMes = FlagMes;
        this.setText(text);
        this.setVerticalTextPosition(SwingConstants.CENTER);
        this.setHorizontalTextPosition(SwingConstants.CENTER);
        this.setFont(UIUtils.TEXT_HY88);
        this.zdlhJpanel = zdlhJpanel;

        refineOperBtn = new RefineOperBtn("skin/8100226/button/49.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, NewRefiningJframe.getNewRefiningJframe().getRefiningJpanel(), 2, "再次炼化");
    }

    @Override
    public void chooseyes() {

    }

    @Override
    public void chooseno() {

    }

    public static Boolean stop = false;

    @Override
    public void nochoose(MouseEvent e) {

//        Limit limit = TimeLimit.getLimits().getLimit("VIP");
//        if (limit != null) {
//            int residueDay = TimerUtil.residueDay(limit.getTime());
//            if (residueDay <= 0) {
//                ZhuFrame.getZhuJpanel().addPrompt2("月卡用户专享！");
//                return;
//            }
//        }else{
//            ZhuFrame.getZhuJpanel().addPrompt2("月卡用户专享！");
//            return;
//        }

        String refining = zdlhJpanel.refining.getText();

        InternalForm internalForm = FormsManagement.getInternalForm(11);
        if (!internalForm.getFrame().isVisible() && refining.equals("炼化")) {
            ZhuFrame.getZhuJpanel().addPrompt2("请先打开炼化属性面板");
            return;
        }

        if (FlagMes.equals("停止")) {
            stop = true;
            return;
        }

        if (!stop && FlagMes.equals("开始")) {
            stop = true;
            ZhuFrame.getZhuJpanel().addPrompt2("已经开始炼化了");
            return;
        }

        stop = false;

        doRefining(refining);

    }

    private void doRefining(String refining) {
        ThreadUtil.execAsync(() -> {
            try {
                if (stop) {
                    return;
                }
                InternalForm internalForm = FormsManagement.getInternalForm(11);
                if (!internalForm.getFrame().isVisible() && (refining.equals("炼化") || refining.equals("炼器"))) {
                    ZhuFrame.getZhuJpanel().addPrompt2("请先打开炼化属性面板");
                    return;
                }

                RefiningEquiJpanel EJpanel = WorkshopRefiningJframe.getWorkshopRefiningJframe().getWorkshopRefiningJpanel()
                        .getCardJpanel().getEquiJpanel();

                String v = RefiningUtil.detection(EJpanel.goods, EJpanel.getType());

                if (refining.equals("炼化") && ("炼化装备".equals(v) || "炼化仙器".equals(v) || "炼化神兵".equals(v))) {
                    refineOperBtn.cao1(EJpanel.goods, EJpanel.money, 4);
                } else if ("炼器".equals(refining)) {
                    RefinersJpanel rJpanel = WorkshopRefiningJframe.getWorkshopRefiningJframe().getWorkshopRefiningJpanel()
                            .getCardJpanel().getRefinersJpanel();

                    String v1 = rJpanel.detection();
                    if (v1.equals("炼器")) {

                        refineOperBtn.cao1(rJpanel.goods, rJpanel.money, 1);
                    } else {
                        ZhuFrame.getZhuJpanel().addPrompt2("炼器公式不对");
                        return;
                    }
                }

                Thread.sleep(1000L);

                int max = 0;
                int current = 0;

                String attribute1Text = zdlhJpanel.attribute1.getText();
                String attribute1_1Text = zdlhJpanel.attribute1_1.getText();

                String attribute2Text = zdlhJpanel.attribute2.getText();
                String attribute2_1Text = zdlhJpanel.attribute2_1.getText();

                String attribute3Text = zdlhJpanel.attribute3.getText();
                String attribute3_1Text = zdlhJpanel.attribute3_1.getText();

                String attribute4Text = zdlhJpanel.attribute4.getText();
                String attribute4_1Text = zdlhJpanel.attribute4_1.getText();

                String attribute5Text = zdlhJpanel.attribute5.getText();
                String attribute5_1Text = zdlhJpanel.attribute5_1.getText();

                String stunt1Text = zdlhJpanel.stunt1.getText();
                String stunt2Text = zdlhJpanel.stunt2.getText();

                if (refining.equals("炼化") || refining.equals("炼器")) {

                    if (StrUtil.isNotBlank(attribute1Text) && StrUtil.isBlank(attribute1_1Text)) {
                        ZhuFrame.getZhuJpanel().addPrompt2("属性一请填写完整");
                        return;
                    }

                    if (StrUtil.isNotBlank(attribute2Text) && StrUtil.isBlank(attribute2_1Text)) {
                        ZhuFrame.getZhuJpanel().addPrompt2("属性二请填写完整");
                        return;
                    }

                    if (StrUtil.isNotBlank(attribute3Text) && StrUtil.isBlank(attribute3_1Text)) {
                        ZhuFrame.getZhuJpanel().addPrompt2("属性三请填写完整");
                        return;
                    }

                    if (StrUtil.isNotBlank(attribute4Text) && StrUtil.isBlank(attribute4_1Text)) {
                        ZhuFrame.getZhuJpanel().addPrompt2("属性四请填写完整");
                        return;
                    }

                    if (StrUtil.isNotBlank(attribute1Text) && StrUtil.isNotBlank(attribute1_1Text)) {
                        max++;
                    }
                    if (StrUtil.isNotBlank(attribute2Text) && StrUtil.isNotBlank(attribute2_1Text)) {
                        max++;
                    }
                    if (StrUtil.isNotBlank(attribute3Text) && StrUtil.isNotBlank(attribute3_1Text)) {
                        max++;
                    }
                    if (StrUtil.isNotBlank(attribute4Text) && StrUtil.isNotBlank(attribute4_1Text)) {
                        max++;
                    }

                    if (!refining.equals("炼器")) {
                        if (StrUtil.isNotBlank(stunt1Text)) {
                            max++;
                        }
                        if (StrUtil.isNotBlank(stunt2Text)) {
                            max++;
                        }
                    } else {
                        if (StrUtil.isNotBlank(attribute5Text) && StrUtil.isBlank(attribute5_1Text)) {
                            ZhuFrame.getZhuJpanel().addPrompt2("属性五请填写完整");
                            return;
                        }
                        if (StrUtil.isNotBlank(attribute5Text) && StrUtil.isNotBlank(attribute5_1Text)) {
                            max++;
                        }
                    }
                }

                if (max == 0) {
                    ZhuFrame.getZhuJpanel().addPrompt2("至少填写一个炼化属性");
                    return;
                }

                List<RefiningValue> values = new ArrayList<>();

                for (int i = 0; i < NewRefiningJpanel.values.size(); i++) {
                    RefiningValue value = NewRefiningJpanel.values.get(i);
                    if (value.isNowAttr() && !refining.equals("炼器")) {
                        //已有属性
                        continue;
                    }
                    values.add(value);
                }

                Map<Integer, Integer> map = new HashMap<>();

                for (RefiningValue value : values) {
                    String valueV2 = value.getV2();
                    String valueV1 = value.getV1();
                    //system.out.println("value2 = " + valueV2);
                    //system.out.println("valueV1 = " + valueV1);
                    if (valueV2.contains("%")) {
                        valueV2 = valueV2.replace("%", "");
                    }
                    double v2 = 0D;
                    if (!valueV1.equals("特技")) {
                        v2 = Double.parseDouble(valueV2);
                    }
                    if (map.get(1) == null && StrUtil.isNotBlank(attribute1Text) && attribute1Text.trim().equals(valueV1) && v2 >= Double.parseDouble(attribute1_1Text)) {
                        map.put(1, 1);
                        current++;
                    } else if (map.get(2) == null && StrUtil.isNotBlank(attribute2Text) && attribute2Text.trim().equals(valueV1) && v2 >= Double.parseDouble(attribute2_1Text)) {
                        map.put(2, 1);
                        current++;
                    } else if (map.get(3) == null && StrUtil.isNotBlank(attribute3Text) && attribute3Text.trim().equals(valueV1) && v2 >= Double.parseDouble(attribute3_1Text)) {
                        map.put(3, 1);
                        current++;
                    } else if (map.get(4) == null && StrUtil.isNotBlank(attribute4Text) && attribute4Text.trim().equals(valueV1) && v2 >= Double.parseDouble(attribute4_1Text)) {
                        map.put(4, 1);
                        current++;
                    } else if (map.get(5) == null && StrUtil.isNotBlank(stunt1Text) && stunt1Text.trim().equals(valueV2)) {
                        map.put(5, 1);
                        current++;
                    } else if (map.get(6) == null && StrUtil.isNotBlank(stunt2Text) && stunt2Text.trim().equals(valueV2)) {
                        map.put(6, 1);
                        current++;
                    }
                }

                if (max != current) {
                    doRefining(refining);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("炼化完成");
                }

            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        });
    }

}
